$(function(){
    //需求 1.点击切换登录注册
    $('.login a').click(function(){
      $('.login').hide() 
      $('.reg').show()
    })
    $('.reg a').click(function(){
        $('.reg').hide() 
        $('.login').show()
      })

    //需求2.自定义属性输入框验证
    layui.form.verify({
        name:[
            /^\w{1,10}$/,
            '请输入1到10位的数字或字母或_'
        ],
        pas:[
            /^\S{6,12}$/,
            '请输入6到12位,且非空格'
        ],
        repas:function(value){
            if(value!=$('.reg [name=password]').val()){
                return '两次输入密码不一致'
            }
        }
    })

    //需求3.注册功能
    $('.reg>form').on('submit',function(e){
        e.preventDefault()
        axios({
            url:'/api/reguser',
            method:'post',
            data:$(this).serialize()
        }).then(res=>{
            //成功回调
            let {data:{status,message}} = res
            if (status != 0) {
                return layui.layer.msg(message)
            }
            layui.layer.msg('恭喜您,注册成功')
            $('.reg a').click()
        })
    })

    //需求4. 登录功能
    $('.login>form').on('submit',function(e){
        e.preventDefault()
        axios({
            url:'/api/login',
            method:'post',
            data:$(this).serialize()
        }).then(res=>{
            //成功回调
            let {data:{status,message,token}} = res
            if (status != 0) {
                return layui.layer.msg(message)
            }
            layui.layer.msg('恭喜您,登录成功')
            localStorage.setItem('token',token)
            setTimeout(function(){
                location.href = '/index.html'
            },1000)
        })
    })
})